<template>
  <div class="container">
    <div class="header">不良项目集计</div>
    <div class="echart-container">
      <div ref="echartsBox" class="echarts-box"></div>
    </div>
    <div class="tips">
      <div v-for="(item, idx) in defectDataDays" :key="idx" class="item">
        <span :style="{ backgroundColor: colorMap[idx] }"></span>
        <div>{{ item }}</div>
      </div>
      <div class="item">
        <span style="background-color: rgba(255, 156, 93, 1)"></span>
        <div>不良率</div>
      </div>
    </div>
  </div>
</template>

<script>
  import commomOption from './commomOption.js';
  import * as echarts from 'echarts';

  export default {
    props: {
      data: {
        default: () => {
          return {
            xDaysTicket: [],
            lineData: [[], []],
            defectDataDaysA: [],
            defectDataDaysB: [],
          };
        },
        typeof: Object,
      },
    },
    data() {
      return {
        defectDataDays: [
          '引线焊接不良',
          '封口板焊接不良',
          '漏电不良',
          '引线折曲不良',
          '封口版插入不良',
          '外观不良',
          '其他不良',
          '卷偏不良',
          '群拔脱落损伤等不良',
        ],

        myChart: null,
        colorMap: [
          '#D59917',
          '#01FFD2',
          '#0178FF',
          '#54B4F7',
          '#AE73EE',
          '#FFF47A',
          '#e4d354',
          '#2b908f',
          '#f45b5b',
          '#91e8e1',
          '#058DC7',
          '#50B432',
          '#ED561B',
          '#DDDF00',
          '#24CBE5',
          '#64E572',
          '#FFF263',
          '#6AF9C4',
          '#D47F00',
          '#00FFFF',
          '#D4FF55',
          '#4572A7',
          '#AA4643',
          '#89A54E',
          '#80699B',
          '#3D96AE',
          '#DB843D',
          '#92A8CD',
          '#D09917',
          '#00FFD2',
          '#0078FF',
          '#50B4F7',
          '#A073EE',
          '#F0F47A',
          '#e0d354',
          '#20908f',
          '#f05b5b',
          '#90e8e1',
          '#008DC7',
          '#54B432',
          '#E0561B',
          '#D0DF00',
          '#20CBE5',
          '#60E572',
          '#F0F263',
          '#60F9C4',
          '#D07F00',
          '#04FFFF',
          '#D0FF55',
          '#4072A7',
          '#A04643',
          '#80A54E',
          '#84699B',
          '#3096AE',
          '#D0843D',
          '#96A8CD',
        ],
      };
    },

    watch: {
      data: {
        handler() {
          if (
            typeof this.data.defectDataDaysA == 'undefined' ||
            this.data.defectDataDaysA == '' ||
            this.data.defectDataDaysA == null
          ) {
            return;
          }
          this.defectDataDaysA = this.data.defectDataDaysA;
          this.defectDataDaysB = this.data.defectDataDaysB;
          this.defectDataDays = this.data.defectDataDays;
          const series = [
            {
              name: this.data.lineData[0][0],
              data: this.data.lineData[0].slice(1),
              type: 'line',
              smooth: true,
              itemStyle: {
                color: 'rgba(255, 156, 93, 1)',
                borderColor: 'rgba(32, 255, 231, 1)',
              },
              label: {
                normal: {
                  // 重写未完成label，通过计算属性重新给formatter赋值，返回
                  show: true, // 计算的总数
                  position: 'top',
                  formatter: '{c}%',
                  textStyle: { color: '#3EFFE8' },
                },
              },
              yAxisIndex: 1,
              lineStyle: {
                width: 2,
              },
            },
            {
              name: this.data.lineData[1][0],
              data: this.data.lineData[1].slice(1),
              type: 'line',
              smooth: true,
              itemStyle: {
                color: 'rgba(255, 156, 93, 1)',
                borderColor: 'rgba(32, 255, 231, 1)',
              },
              label: {
                normal: {
                  // 重写未完成label，通过计算属性重新给formatter赋值，返回
                  show: true, // 计算的总数
                  position: 'top',
                  formatter: '{c}%',
                  textStyle: { color: '#3EFFE8' },
                },
              },
              yAxisIndex: 1,
              lineStyle: {
                width: 2,
              },
            },
          ];
          this.data.defectDataDaysA.forEach((dataItem, idx) => {
            series.push({
              name: dataItem[0],
              yAxisIndex: 0,
              type: 'bar',
              barWidth: 15,
              stack: 'stackName',
              emphasis: {
                focus: 'series',
              },
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: this.colorMap[idx] },
                  { offset: 1, color: 'rgba(1, 25, 46, 0.7)' },
                ]),
              },
              data: dataItem.slice(1),
            });
          });
          this.data.defectDataDaysB.forEach((dataItem, idx) => {
            series.push({
              name: dataItem[0],
              yAxisIndex: 0,
              type: 'bar',
              barWidth: 15,
              stack: 'stackName1',
              emphasis: {
                focus: 'series',
              },
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: this.colorMap[idx] },
                  { offset: 1, color: 'rgba(1, 25, 46, 0.7)' },
                ]),
              },
              data: dataItem.slice(1),
            });
          });
          const option = {
            //tooltip: commomOption.tooltip,
            tooltip: {
              trigger: 'item',
              enterable: true, //设置鼠标 是否可以进入浮框中
              confine: true, //设置 浮框 限制在 图表的区域内
              extraCssText: 'max-height:90%;overflow:scroll', //设置最大高度，并添加滚动
            },
            emphasis: {
              show: true,
            },
            grid: {
              top: 8,
              bottom: 20,
              left: 50,
              right: 50,
            },
            xAxis: Object.assign({}, commomOption.xAxis, {
              data: this.data.xDaysTicket,
            }),
            yAxis: [
              {
                type: 'value',
                ...commomOption.yAxis,
              },
              {
                type: 'value',
                min: 0,
                axisLabel: {
                  show: true,
                  interval: 'auto',
                  color: 'rgb(255, 255, 255)',
                  formatter: '{value}%',
                },
              },
            ],
            series: series,
          };
          this.echartInit(option);
        },
        immediate: true,
      },
    },

    methods: {
      // echarts初始化
      echartInit(option) {
        if (this.myChart) {
          this.myChart.setOption(option);
          return false;
        }

        this.$nextTick(() => {
          this.myChart = echarts.init(this.$refs.echartsBox);

          option && this.myChart.setOption(option);
          // 根据页面大小自动响应图表大小
          window.addEventListener('resize', () => {
            this.myChart.resize();
          });
        });
      },
    },
  };
</script>
<style lang="scss" scoped>
  .container {
    position: relative;
    flex: 1;
    //background: url(../image/quality/rbg.png) 0px 0px no-repeat;
    background-size: 100% 100%;
    width: 100%;
    min-height: 500px;
    display: flex;
    flex-direction: column;
    padding-bottom: 10px;
    &:nth-last-child(1) {
      padding-bottom: 0px;
    }
    .header {
      font-size: 20px;
      font-family: 'Microsoft YaHei';
      color: rgb(255, 255, 255);
      font-weight: bold;
      text-shadow: 0px 0px 10px rgba(5, 28, 67, 0.004);
      margin-top: 20px;
      padding-bottom: 5px;
      padding-left: 6%;
      background: url(../image/block_title_2.png) 0px 0px no-repeat;
      background-size: 100% 100%;
    }
    .echart-container {
      height: 600px !important;
      padding: 1px 1px 0px;
      background-color: rgba(7, 45, 76, 0.3);
    }
    .echarts-box {
      width: 100%;
      height: 100%;
    }
    .tips {
      flex: 1;
      height: 100%;
      padding: 20px;
      display: flex;
      flex-wrap: wrap;

      .item {
        width: 24.5%;
        display: flex;
        align-items: center;
        //justify-content: center;
        font-size: 20px;
        color: #ffffff;
        line-height: 5px;
        padding: 5px 5px;

        span {
          display: inline-block;
          width: 10px;
          height: 10px;
          margin-right: 5px;
        }
      }
    }
  }
</style>
